package com.waimai.db.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.waimai.db.dao.OrderDao;
import com.waimai.db.dao.ReceiveAddressDao;
import com.waimai.db.dao.UserDao;
import com.waimai.db.entity.Comment;
import com.waimai.db.entity.Order;
import com.waimai.db.entity.OrderStatus;
import com.waimai.db.entity.ReceiveAddress;
import com.waimai.db.entity.User;
import com.waimai.db.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	public UserDao userDao;

	@Autowired
	public OrderDao orderDao;

	@Autowired
	public ReceiveAddressDao addressDao;

	public User checkUser(User user) {
		return userDao.findUser(user);
	}

	public User addUser(User user) {
		user.setBalance((float) 0.0);
		return userDao.insert(user);
	}

	public User checkPhone(String phone) {
		return userDao.findUser(new User(phone));
	}

	public User updateUser(User user) {
		if (user.getPasswordOld() != null) {
			User userTemp = userDao.findUser(user);
			if (userTemp.getPassword().equals(user.getPasswordOld())) {
				return userDao.update(user);
			} else {
				return null;
			}
		}
		return userDao.update(user);
	}

	public List<ReceiveAddress> getAddresses(Integer userId) {
		return userDao.getAddresses(userId);
	}

	public Order getOrder(Integer orderId) {
		return orderDao.findById(orderId);
	}

	public List<Order> getOrders(Integer userId) {
		return orderDao.getUserOrders(userId);
	}

	public ReceiveAddress addAddress(ReceiveAddress address) {
		return addressDao.addAddress(address);
	}

	public ReceiveAddress updateAddress(ReceiveAddress address) {
		return addressDao.updateAddress(address);
	}

	public ReceiveAddress deleteAddress(Integer addressId) {
		return addressDao.deleteAddress(addressId);
	}

	public Comment addComment(Integer orderId, Comment comment) {
		orderDao.updateStatus(orderId, new OrderStatus(
				OrderStatus.STATUS_COMPLITED));
		return orderDao.addComment(orderId, comment);
	}

	public List<User> getUsers() {
		// TODO Auto-generated method stub
		return userDao.getUsers();
	}
	
	public List<Map<String, Object>> getReceiveAddresses(){
		return addressDao.getReceiveAddresses();
	}

	public List<Map<String, Object>> getCommentsByUserId(int userId) {
		// TODO Auto-generated method stub
		return userDao.getCommentsByUserId(userId);
	}
}
